﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using MODEL;
using System.Transactions;
using System.Data.OleDb;
namespace winUi
{
    public partial class FormTuiKu : Form
    {
        private Dic dic =new Dic();
        private ChuKu c = new ChuKu();
        public FormTuiKu()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string namePY = txtKey.Text;
            LoadDataByPy(namePY);

        }

        private void FormRuKU_Load(object sender, EventArgs e)
        {
            LoadDataByPy("");
            dgvStock.MultiSelect = false;
            dgvStock.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dtpDate.CustomFormat = "YYYY-MM-dd";
            dtpDate.Value = DateTime.Now.Date;
            dgvStock.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dgvStock.MultiSelect = false;
            dgvStock.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

        }

        private void dgvDic_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && dgvStock.Focused)
            {
                DataGridViewRow row = dgvStock.CurrentRow;
                int dicid = Convert.ToInt32(row.Cells[2].Value);
                dic = new BllDic().GetById(dicid);
                c = new BllChuKu().GetById(dicid);
                txtName.Text = dic.GoodsName;
                txtModel.Text = dic.GoodsModel;
                txtSpe.Text = dic.GoodsSpe;
                txtUnit.Text = dic.GoodsUnit;
                txtPrice.Text = dic.GoodsPrice.ToString();
                txtNumber.Text = c.Number.ToString();
                txtRemark.Focus();
            }
            return;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string namePy = txtKey.Text;
            LoadDataByPy(namePy);
        }
        public void LoadDataByPy(string namePy)
        {
            dgvStock.DataSource = null;
            dgvStock.DataSource = new BllChuKu().GetListByPy(namePy);
        }
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtKey.Focused && e.KeyCode == Keys.Enter)
            {
                dgvStock.Focus();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            TuiKu t = new TuiKu();
            t.ChuKuId = c.Id;
            t.DicId = c.DicId;
            t.CreateDate = dtpDate.Value;
            t.GoodPy = c.PY;
            t.GoodsNumber = c.Number;
            t.TuiRemark = txtRemark.Text;
            c.Remark = txtRemark.Text;
            try
            {
                    new ServiceRuKu().TuiKu(t);
                    MessageBox.Show("成功");
                    LoadDataByPy("");
                    clear();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
           
        }
        public void clear()
        {
            txtModel.Text = "";
            txtName.Text = "";
            txtNumber.Text = "0";
            txtPrice.Text = "0.00";
            txtRemark.Text = "";
            txtSpe.Text = "";
            txtUnit.Text = "";
            txtKey.Focus();
        }

        private void txtNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && txtNumber.Focused)
            {
                txtPrice.Focus();
            }
            
        }

        private void txtPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && txtPrice.Focused)
            {
                txtRemark.Focus();
            }
        }

        private void txtRemark_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && txtRemark.Focused)
            {
                btnSave.Focus();
            }
        }

        private void dtpDate_Enter(object sender, EventArgs e)
        {
            
        }

        private void txtNumber_TextChanged(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(txtNumber.Text)&&Convert.ToInt32(txtNumber.Text) > stock.Number)
            //{
            //    txtNumber.BackColor = Color.Red;
            //    //txtStockNum.BackColor = Color.Red;
            //}
            //else
            //{
            //    txtNumber.BackColor = Color.White;
            //    //txtStockNum.BackColor = Color.White;
            //}
            
        }
       
    }
}
